package com.yunkaweilai.android.activity.member;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.fragment.member.FreeRechargeFragment;
import com.yunkaweilai.android.fragment.member.PreferentiaActivitiesFragment;
import com.yunkaweilai.android.fragment.member.PremiumRechargeFragment;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.utils.InputMethodUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 充值方式
 */
public class RechargeModeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.id_group)
    RadioGroup idGroup;
    @BindView(R.id.id_member_container)
    FrameLayout idShopContainer;
    @BindView(R.id.id_rbtn_1)
    RadioButton idRbtn1;
    public static final String Recharge_Mode_MEMBER_MODEL = "Recharge_MEMBER_MODEL";
    public static final String Recharge_Mode_MEMBER_BUNDLE = "Recharge_MEMBER_BUNDLE";
    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tvcard_number)
    TextView idTvcardNumber;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_all_llayout)
    LinearLayout idAllLlayout;
    @BindView(R.id.id_rbtn_2)
    RadioButton idRbtn2;
    @BindView(R.id.id_rbtn_3)
    RadioButton idRbtn3;
    @BindView(R.id.id_view1)
    View idView1;
    @BindView(R.id.id_view2)
    View idView2;
    @BindView(R.id.id_view3)
    View idView3;
    private FreeRechargeFragment freeFragment;//随意充值
    private PreferentiaActivitiesFragment preferentiaActivitiesFragment;//优惠活动
    private PremiumRechargeFragment premiumRechargeFragment;//优惠充值
    private MemberHeadModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_mode);
        ButterKnife.bind(this);
        model = (MemberHeadModel) getIntent().getBundleExtra(Recharge_Mode_MEMBER_BUNDLE).getSerializable(Recharge_Mode_MEMBER_MODEL);
        initHeadView();
        idGroup.setOnCheckedChangeListener(this);
        idRbtn1.setChecked(true);
        idAllLlayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    InputMethodUtils.closeSoftKeyboard(mAc);
                }
                return false;
            }
        });
    }

    //头部信息
    private void initHeadView() {
        new TitleBuilder(mAc).setTitleText("充值方式").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        Utils.MemberHeadImg(mAc,model.getMember_avatar(),model.getMember_sex(),idImgHead);

        idTvName.setText(model.getMember_name() + " (" + model.getLevel_name() + ": " + model.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + model.getDiscount() / 10 + "折   积分：" + model.getMember_points() + "   余额：" + model.getMember_price());
    }

    public void hideAllFragment(FragmentTransaction transaction) {
        if (freeFragment != null) {
            transaction.hide(freeFragment);
        }
        if (preferentiaActivitiesFragment != null) {
            transaction.hide(preferentiaActivitiesFragment);
        }
        if (premiumRechargeFragment != null) {
            transaction.hide(premiumRechargeFragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (checkedId) {
            case R.id.id_rbtn_1:
                if (freeFragment == null) {
                    freeFragment = new FreeRechargeFragment();
                    transaction.add(R.id.id_member_container, freeFragment);
                } else {
                    transaction.show(freeFragment);
                }
                idView1.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView2.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                idView3.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
            case R.id.id_rbtn_2:
                if (preferentiaActivitiesFragment == null) {
                    preferentiaActivitiesFragment = PreferentiaActivitiesFragment.getInstence(model);
                    transaction.add(R.id.id_member_container, preferentiaActivitiesFragment);
                } else {
                    transaction.show(preferentiaActivitiesFragment);
                }
                idView2.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView1.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                idView3.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
            case R.id.id_rbtn_3:
                if (premiumRechargeFragment == null) {
                    premiumRechargeFragment = PremiumRechargeFragment.getInstence(model);
                    transaction.add(R.id.id_member_container, premiumRechargeFragment);
                } else {
                    transaction.show(premiumRechargeFragment);
                }
                idView3.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView2.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                idView1.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
        }
        transaction.commit();
    }

}
